var url;

function newItem() {
    $('#dlg').dialog('open').dialog('setTitle', 'New Item');
    $('#fm').form('clear');
    url = '/item/newItem';

}

function saveItem() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function () {
            alert("提交成功!!!");
            $('#dlg').dialog('close');		// close the dialog
            $('#dg').datagrid('reload');	// reload the user data
        }
    });
}

function editItem() {
    var row = $('#dg').datagrid('getSelected');
    alert(row.id + "," + row.detail);
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', "Edit Item");
        $('#fm').form('load', row);
        url = '/item/updateItem/' + row.id;
    }
}

function destroyItem() {
    var rows = $('#dg').datagrid('getSelections');
    var ids = "";
    if (rows) {
        for (var i = 0; i < rows.length; i++) {
            if (ids === "") {
                ids = rows[i].id;
            } else {
                ids = rows[i].id + "," + ids;
            }
        }
    }
    $.messager.confirm('Confirm', 'Are you sure you want to destroy this item?'
        , function () {
            $.ajax({
                type: 'post',
                url: '/item/deleteItems',
                data: {"ids": ids},
                success: function () {
                    alert("成功删除" + rows.length + "条记录!");
                    $('#dg').datagrid('reload');	// reload the user data
                },
                error:function(){
                    alert("删除失败!");
                }
            });
        });
}

function doSearch() {
    $('#tt').datagrid('load', {
        name: $('#name').val()
    });
}